//
//  NewFeatureCollectionViewController.swift
//  weibo-01
//
//  Created by 韩家奇 on 15/11/7.
//  Copyright © 2015年 hanjiaqi. All rights reserved.
//

import UIKit
import SnapKit
//可重用cell的重用Id
private let NewFeatureViewCellId = "NewFeatureViewCellId"
//新特性图像的数量
private let NewFeatureViewCount = 4

class NewFeatureCollectionViewController: UICollectionViewController {
    
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        //构造函数  完成之后内部属性才会被创建
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 注册可重用cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: NewFeatureViewCellId)
    }

    //每个分组中格子的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewFeatureViewCount
    }
    //cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureViewCellId, forIndexPath: indexPath) as! NewFeatureCell
        cell.imgeIndex = indexPath.item

        return cell
    }
    // scrollview 停止滚动的方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //最后一页调滚动方法
        //根据偏移量计算页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        //判断是否是最后一页
            if page != NewFeatureViewCount - 1 {
                return
            }
            //播放动画
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
            //显示动画
        cell.showButtonAnim()
        }
}


// MARK: 新特性cell
private class NewFeatureCell : UICollectionViewCell {
    //图像属性
    private var imgeIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imgeIndex + 1)")
            //隐藏按钮
            startButton.hidden = true
        }
    }
    //点击开始体验按钮
    @objc private func clickStratButton() {
        print("开始体验")
        NSNotificationCenter.defaultCenter().postNotificationName(HQSwitchRootViewControllerNotification, object: nil)
    }
    //frme 的小大是 layou.itemSize 指定的
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //显示按钮动画
    private func showButtonAnim() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(2.0,//动画持续时间
            delay: 0,//延迟时间
            usingSpringWithDamping: 0.7,//弹力系数 越小约弹
            initialSpringVelocity: 10, //初始速度,模拟重力加速
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    //添加控件
    private func setupUI() {
        //1添加控件
         addSubview(iconView)
        addSubview(startButton)
        //2指定位置
        iconView.frame = bounds
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
            //3 监听方法
            startButton.addTarget(self, action: "clickStratButton", forControlEvents: UIControlEvents.TouchUpInside)
        }
    }
    // MARK: - 懒加载空间
    //图像
    private lazy var iconView: UIImageView = UIImageView()
    private lazy var startButton:UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backimageName: "new_feature_finish_button")

}
